const Koa = require('koa')
// 实例化后得到koa实例对象
const app = new Koa()
// 绑定端口
app.listen(3000, () => console.log('http://localhost:3000'))

// 路由
/* app.use(async (ctx, next) => {
  // console.log(ctx.request.path)
  const pathname = ctx.request.path
  if ('/home' === pathname) {
    ctx.body = '我是home页面'
  } else {
    ctx.status = 404
    ctx.body = '页面丢失'
  }
}) */

const router = {
  home(ctx){
    ctx.body = '我是一个HOME页面'
  }
}

app.use(async (ctx, next) => {
  const pathname = ctx.request.path
  try {
    router[pathname.slice(1)](ctx)
  } catch (error) {
    ctx.status = 404
    ctx.body = '页面丢失'
  }
})
